package othello.gui;

import java.awt.Component;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import othello.util.GameDetails;

public class ResultsPanel extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;
	private ConcurrentHashMap<Integer, JLabel> map = new ConcurrentHashMap<Integer, JLabel>();

    /**
     * Set layout
     */
    public ResultsPanel() {
        BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
        setLayout(layout);
    }

    /**
     * Update result panel when players have won a game.
     *
     * Results are rendered as:
     * A - B (won by A - won by B - tie)
     * C - D (won by A - won by B - tie)
     *
     * @param obj : GameDetails
     * @param arg : null
     */
    @Override
    public void update(Observable obj, Object arg) {
        GameDetails gd = ((GameDetails) obj);
        int id = gd.getId();
        if (map.containsKey(id)) {
            map.get(id).setText(gd.toString());
        } else {
            JLabel l = new JLabel(gd.toString());
            l.setAlignmentX(Component.CENTER_ALIGNMENT);
            add(l);
            map.put(id, l);
        }
        validate();
        repaint();
        gd = null;
    }
}
